@extends('Admin.base')
@section('css')
    <link rel="stylesheet" href="/Admin/layuiadmin/css/extend/formSelects-v4.css" media="all">
@endsection
@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>编辑</legend>
            </fieldset>
        </div>
        <div class="layui-card-body">
{{--            <form class="layui-form" id="layui-form" action="{{route('adminer.update',['id' => $data->id])}}" method="post">--}}
            <form class="layui-form" id="layui-form" action="{{route('goods.update',['id' => $goods])}}" method="post">
                @include('Admin.goods._form')
            </form>
        </div>
    </div>
@endsection
@section('script')
    <script src="/js/ajax.js"></script>
    {{--<script src="/Admin/assets/ckeditor411/ckeditor.js"></script>--}}
    <script>



        // function delbanner(){
        //     var id = obj.getAttribute('id');
        //     var dd = document.getElementById(id);
        //     var d = document.getElementsByName('banner['+ id +']');
        //     dd.remove();
        //     obj.remove();
        //     d[0].remove();
        //     $("#uploadBanner").removeAttr('disabled');
        // }

        layui.use(['formSelects','upload','form','layedit'], function(){

            var $ = layui.jquery;
            var upload = layui.upload;
            var layedit = layui.layedit;
            var form = layui.form;

//            $(".clearbanner").click(function(){
//                var id = $(this).attr("id");
//                console.log(id);
//                $("div[data='"+ id + "'").remove();
//            });
            upload.render({
                elem: '#uploadPhoto'
                ,url: '{{route('goods.uploadphoto')}}'
                ,field: 'photo' //设定文件域的字段名
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#uploadPhoto-img').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(res){

                    if(res.code > 0){
                        $("#photo").val(res.data);
                        return layer.msg('上传成功');
                    }

                    return layer.msg('上传失败');
                }
                ,error: function(){

                }
            });

            //多图片上传
            upload.render({
                elem: '#uploadBanner'
                ,url: '{{route('goods.uploadphoto')}}'
                ,multiple: true
                ,field: 'banner' //设定文件域的字段名
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        if ($(".banner").length>=5){
                            $("#uploadBanner").attr({"disabled":"disabled"});
                            return false;
                        }
                    });
                }
                ,done: function(res){
                    if ($(".banner").length>=5){
                        $(".baninput").last().val(res.data);
                        $("#uploadBanner").attr({"disabled":"disabled"});
                        return layer.msg('上传banner图片不准超过5个');
                    }
                    if(res.code > 0){
                        $('#uploadBanner-imgs').append(
                                '<div class="layui-inline">' +
                                '<img src="'+ res.data +'" class="banner"  width="50px" height="50px"><br>' +
                                '<p  style="width: 50px" class="layui-btn layui-btn-green layui-btn-xs clearbanner">移 除</p>' +
                                '<input class="baninput" type="hidden"  name="banner[]" value="' + res.data + '" >' +
                                '</div>')
                        //$(".baninput").last().val(res.data);
                        return layer.msg('上传成功');
                    }

                    return layer.msg('上传失败');
                }
            });

            layedit.set({
                uploadImage: {
                    url: '{{route('goods.uploadphoto')}}'
                    ,type: 'post'

                }
            });
            var editIndex = layedit.build('description');

            form.verify({
                description: function(value){
                    layedit.sync(editIndex);
                }
            });


        });

        //移除图片
        $(document).on("click", ".layui-inline .clearbanner", function (event) {
            $(this).parent().remove();
        });
    </script>
@endsection
